﻿package tg.app.rj.workticket;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import psdi.common.action.ActionCustomClass;
import psdi.mbo.Mbo;
import psdi.mbo.MboRemote;
import psdi.server.MXServer;
import psdi.util.MXApplicationException;
import psdi.util.MXException;

//
//	流程节点操作：许可人撤销的正向操作
//
public class WT_YHCX implements ActionCustomClass {

	public void applyCustomAction(MboRemote mboremote, Object[] aobj)
			throws MXException, RemoteException {
		CustWorkTicketRemote wtmboremote = (CustWorkTicketRemote) mboremote;
		String user = wtmboremote.getUserInfo().getDisplayName();
		Date date = MXServer.getMXServer().getDate();
		String status = wtmboremote.getString("status");
		WT(wtmboremote, date, status, user);
	}

	private void WT(CustWorkTicketRemote wtmboremote, Date date, String status,
			String user) throws MXException, RemoteException {
		if (xkrcheckFZR(wtmboremote, date, wtmboremote.getString("WT2"),
				wtmboremote.getString("WT4")).equals("yes")) {
			throw new MXApplicationException("workticket", "SUPERVISOR");
		}
		wtmboremote.setValue("WT34", "0", 11L);
		wtmboremote.setValue("WT77", date, 11L);
		wtmboremote.setValue("WT78", user, 11L);
		wtmboremote.changeStatus("FZRJXJL", date, "工作流更改WAPPR到FZRJXJL");

	}

	protected String xkrcheckFZR(CustWorkTicketRemote wtmboremote, Date date,
			String FZR, String bz) throws MXException, RemoteException {
		Mbo mbo = (Mbo) wtmboremote.getThisMboSet().getMbo();
		Statement statement = null;
		ResultSet resultset = null;
		String countNum = "no";
		try {
			Connection connection = mbo.getMboServer().getDBConnection(
					mbo.getUserInfo().getConnectionKey());
			String s6 = "select COUNT(*) from (select decode(WT25,'',WT2,WT25) WT2,WT25,status,wtnum,wt34,wttype,wt4 from workticket) where status not in ('WAPPR','INIT','FINIT','REC','SAFPMT','PMT','CAN','GZZJ','CLOSE') "
					+ "AND wttype in ('D1','D2','RJ','RK','JD','CC') AND wt34 ='0' AND WT2='"
					+ FZR + "' AND WT4='" + bz + "'";// 在许可人这里判断当前负责人是否只有一张票，除动火票和已被押回外的票，已许可未终结的票！//工作票押回
			statement = connection.createStatement();
			resultset = statement.executeQuery(s6);

			if (resultset.next()) {
				if (!(resultset.getString(1).equals("0"))) {
					countNum = "yes";
				}
			}
		} catch (Exception exception) {
			exception.printStackTrace();
			try {
				if (resultset != null) {
					resultset.close();
				}

			} catch (SQLException localSQLException) {
			}

		} finally {
			try {
				resultset.close();
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			mbo.getMboServer().freeDBConnection(
					mbo.getUserInfo().getConnectionKey());
		}
		return countNum;
	}
}
